package com.skivingcloud.report.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.report.constants.ReportConstant;
import com.skivingcloud.report.entity.ReportDatasource;
import com.skivingcloud.report.entity.ReportType;
import com.skivingcloud.report.mapper.ReportTypeMapper;
import com.skivingcloud.report.service.ReportTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.skivingcloud.common.utils.PageParameter;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author skivingcloud
 * @since 2024-11-07
 */
@Service
public class ReportTypeServiceImpl extends ServiceImpl<ReportTypeMapper, ReportType> implements ReportTypeService {

    /**
     * 分页查询
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<ReportType> queryByPage(PageParameter<ReportType> parameter){
        Page<ReportType> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (StringUtils.isNoneBlank(parameter.getParam().getId())) {
                queryWrapper.and(ReportType::getId).like(parameter.getParam().getId());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getTenantId())) {
                queryWrapper.and(ReportType::getTenantId).eq(parameter.getParam().getTenantId());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getReportTypeName())) {
                queryWrapper.and(ReportType::getReportTypeName).like(parameter.getParam().getReportTypeName());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getCreator())) {
                queryWrapper.and(ReportType::getCreator).like(parameter.getParam().getCreator());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getUpdater())) {
                queryWrapper.and(ReportType::getUpdater).like(parameter.getParam().getUpdater());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getDelFlag())) {
                queryWrapper.and(ReportType::getDelFlag).like(parameter.getParam().getDelFlag());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 全量查询数据
     *
     * @return 数据
     */
    @Override
    public List<ReportType> getAllReportType() {
        QueryWrapper queryWrapper = QueryWrapper.create().where(ReportType::getTenantId).eq(UserUtil.getCurrentUser().getTenantId())
                .and(ReportType::getDelFlag).eq(ReportConstant.COMMON_YES_OR_NO_NO);
        return this.mapper.selectListByQuery(queryWrapper);
    }
}
